package com.atguigu.service.impl;

import com.atguigu.dao.TomcatServiceConfigDao;
import com.atguigu.dao.impl.TomcatServiceConfigDaoImpl;
import com.atguigu.pojo.TomcatServiceConfig;
import com.atguigu.pojo.Page;
import com.atguigu.service.TomcatServiceConfigService;

import java.util.List;

public class TomcatServiceConfigServiceImpl implements TomcatServiceConfigService {
    TomcatServiceConfigDao tomcatServiceConfigDao=new TomcatServiceConfigDaoImpl();
    @Override
    public void addTomcatServiceConfig(TomcatServiceConfig tomcatServiceConfig) {
        tomcatServiceConfigDao.addTomcatServiceConfig(tomcatServiceConfig);
    }

    @Override
    public void updateTomcatServiceConfig(TomcatServiceConfig tomcatServiceConfig) {
        tomcatServiceConfigDao.updateTomcatServiceConfig(tomcatServiceConfig);
    }

    @Override
    public void deleteTomcatServiceConfigById(String id) {
        tomcatServiceConfigDao.deleteTomcatServiceConfigById(id);
    }

    @Override
    public TomcatServiceConfig queryTomcatServiceConfigById(String id) {
        return tomcatServiceConfigDao.queryTomcatServiceConfigById(id);
    }

    @Override
    public List<TomcatServiceConfig> queryTomcatServiceConfigs() {
        return tomcatServiceConfigDao.queryTomcatServiceConfigs();
    }
    @Override
    public TomcatServiceConfig queryTomcatServiceConfigByServiceId(String serviceId){
        return tomcatServiceConfigDao.queryTomcatServiceConfigByServiceId(serviceId);
    }
    @Override
    public List<TomcatServiceConfig> queryTomcatServiceConfigsByProjectModuleId(String projectModuleId){
        return tomcatServiceConfigDao.queryTomcatServiceConfigsByProjectModuleId(projectModuleId);
    }
    @Override
    public List<TomcatServiceConfig> queryTomcatServiceConfigsByProjectModuleIdAndDeployEnvId(String projectModuleId,String deployEnvId){
        return tomcatServiceConfigDao.queryTomcatServiceConfigsByProjectModuleIdAndDeployEnvId(projectModuleId,deployEnvId);
    }
    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<TomcatServiceConfig> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=tomcatServiceConfigDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<TomcatServiceConfig> items=tomcatServiceConfigDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
